var apiUrl = $("#apiUrl").val();
var fileCount = 1, dailyCount = 1;
$(document).ready(function(){
	initBase();
	/*getTrainList();
	$("#show-count").change(function(){
        getTrainList();
    })*/
    $("#fileAdd").click(function(){
    	fileCount++;
    	$(".new-file").append('<div style="padding: 0;padding-top:15px;border-top: 1px solid #eee;position: relative" class="col-md-12 file_count_'+fileCount+'"><div class="form-group col-md-6">\
	        <label class="col-md-4 control-label text-muted">文件标题</label>\
	        <div class="col-md-8">\
	            <input type="text" class="form-control" name="fileTitle" id="fileTitle_'+fileCount+'">\
	        </div>\
	    </div>\
	    <div class="form-group col-md-6">\
	        <label class="col-md-4 control-label text-muted">文件内容</label>\
	        <div class="col-md-8">\
	            <input type="text" class="form-control" name="fileContent" id="fileContent_'+fileCount+'">\
	        </div>\
	    </div>\
	    <div class="form-group col-md-6">\
	        <label class="col-md-4 control-label text-muted">文件类型</label>\
	        <div class="col-md-8">\
	            <select class="form-control init-select" style="width: 100%" name="fileType" id="fileType_'+fileCount+'">\
	                <option value=""></option>\
	                <option value="1">图片</option>\
	                <option value="2">pdf</option>\
	                <option value="3">excel</option>\
	                <option value="4">world</option>\
	                <option value="5">视频</option>\
	                <option value="6">超链接</option>\
	            </select>\
	        </div>\
	    </div>\
	    <div class="form-group col-md-6 show-link-'+fileCount+'">\
	        <label class="col-md-4 control-label text-muted">链接地址</label>\
	        <div class="col-md-8">\
	            <input type="text" class="form-control" name="fileLink" id="fileLink_'+fileCount+'">\
	        </div>\
	    </div>\
	    <div class="form-group col-md-6">\
	        <label class="col-md-4 control-label text-muted">文件上传</label>\
	        <div class="col-md-8">\
	            <input type="file" name="files" class="file" id="files_'+fileCount+'" multiple/>\
	        </div>\
	    </div>\
	    <span class="fa fa-remove file-remove" title="删除" id="remove_'+fileCount+'" style="position: absolute"></span>\
	    </div>')
	    $("#fileType_"+fileCount).select2({
	        language: 'zh-CN',
	        placeholder: '请选择',
	        allowClear: true
	    });
	    $("#fileType_"+fileCount).change(function (){
	    	if($("#fileType_"+fileCount).val() == 6){
	    		$(".show-link-"+fileCount).removeClass("hide");
	    	}else{
	    		$(".show-link-"+fileCount).addClass("hide");
	    		$(".show-link"+fileCount+" input").val("");
	    	}
	    })
	    $("#files_"+fileCount).fileinput({
		    language: 'zh', //设置语言
		    enctype: 'multipart/form-data',
		    showUpload: false, //是否显示上传按钮
		    showCaption: false,//是否显示标题
		    browseClass: "btn btn-info", //按钮样式
		    msgFilesTooMany: "选择上传的文件数量({n}) 超过允许的最大数值{m}！"
		});
		$("#remove_"+fileCount).click(function(){
			var index = $(this)[0].id.split("_")[1];
			$(".file_count_"+index).remove();
		})
    })

    $("#dailyAdd").click(function(){
    	dailyCount++;
    	$(".new-daily").append('<div style="padding: 0;padding-top:15px;border-top: 1px solid #eee;position: relative" class="col-md-12 daily_'+dailyCount+'">\
            <div class="form-group col-md-6">\
                <label class="col-md-4 control-label text-muted">开始时间</label>\
                <div class="col-md-8">\
                    <input type="text" class="form-control date-input" name="dailyStartDate" id="dailyStartDate_'+dailyCount+'">\
                </div>\
            </div>\
            <div class="form-group col-md-6">\
                <label class="col-md-4 control-label text-muted">结束时间</label>\
                <div class="col-md-8">\
                    <input type="text" class="form-control date-input" name="dailyEndDate" id="dailyEndDate_'+dailyCount+'">\
                </div>\
            </div>\
            <div class="form-group col-md-6">\
                <label class="col-md-4 control-label text-muted">日常标题</label>\
                <div class="col-md-8">\
                    <input type="text" class="form-control" name="dailyTitle" id="dailyTitle_'+dailyCount+'">\
                </div>\
            </div>\
            <div class="form-group col-md-6">\
                <label class="col-md-4 control-label text-muted">日常内容</label>\
                <div class="col-md-8">\
                    <input type="text" class="form-control" name="dailyContent" id="dailyContent_'+dailyCount+'">\
                </div>\
            </div>\
            <div class="form-group col-md-12" style="position: relative;">\
                <label class="col-md-2 control-label text-muted">备注</label>\
                <div class="col-md-10" style="padding-left: 10px;padding-right: 43px;">\
                    <input type="text" class="form-control" name="dailyRemark" id="dailyRemark_'+dailyCount+'">\
                </div>\
                <span class="fa fa-remove" id="dailyRemove_'+dailyCount+'" style="position: absolute;cursor: pointer;top: 10px;right: 25px;color: #EA544A"></span>\
            </div>\
        </div>')
        $("#dailyRemove_"+dailyCount).click(function(){
			var index = $(this)[0].id.split("_")[1];
			$(".daily_"+index).remove();
		})
		$("#dailyStartDate_"+dailyCount+" ,#dailyEndDate_"+dailyCount).datetimepicker({
	        autoclose: true,
	        todayHighlight:true,
	        language:'zh-CN',
	        forceParse:true,
	        format:'yyyy-mm-dd',
	        minView:2,
	        todayBtn:true
	    })
    })

    $("#fileType_1").change(function (){
    	if($("#fileType_1").val() == 6){
    		$(".show-link").removeClass("hide");
    	}else{
    		$(".show-link").addClass("hide");
    		$(".show-link input").val("");
    	}
    })
})

$("#add-modal").iziModal({
    title: "添加培训",
    headerColor: '#00c0ef',
    rtl: false,
    width: 900,
    zindex: 1044,
    focusInput: false,
    group: '',
    loop: false,
    history: false,
    bodyOverflow: false,
    overlayClose: false,
    onOpening: function(){
    },
    onOpened: function(){
    },
    onClosing: function () {},
    onClosed: function () {
    	fileCount = 1;
    	$("#new-file").html("");
    }
});

$("#daily-modal").iziModal({
    title: "添加日常",
    headerColor: '#00c0ef',
    rtl: false,
    width: 900,
    zindex: 1044,
    focusInput: false,
    group: '',
    loop: false,
    history: false,
    bodyOverflow: false,
    overlayClose: false,
    onOpening: function(){
    },
    onOpened: function(){
    },
    onClosing: function () {},
    onClosed: function () {
    	dailyCount = 1;
    	$(".new-daily").html("");
    }
});

function getTrainList(){
	var listData = {

	}

	publicAjax(apiUrl+"train/list","POST",listData,function(data) {

	})
}

function addTrain(){
	$("#add-modal").iziModal("open");
}

function addDaily(){
	$("#daily-modal").iziModal("open");
}

function initBase(){
	$("#trainType,select[name='fileType']").select2({
        language: 'zh-CN',
        placeholder: '请选择',
        allowClear: true
    });

	$(".date-input").datetimepicker({
        autoclose: true,
        todayHighlight:true,
        language:'zh-CN',
        forceParse:true,
        format:'yyyy-mm-dd',
        minView:2,
        todayBtn:true
    })

	$("#openSignIn").select2({
        language: 'zh-CN'
    });
}

function refresh(){
	window.location.reload();
}

function search(){
	getTrainList()
}

$("input[name='files']").fileinput({
    language: 'zh', //设置语言
    enctype: 'multipart/form-data',
    showUpload: false, //是否显示上传按钮
    showCaption: false,//是否显示标题
    browseClass: "btn btn-info", //按钮样式
    msgFilesTooMany: "选择上传的文件数量({n}) 超过允许的最大数值{m}！"
});